﻿$(function () {
    initlist();

    if ($("#search-submit").length > 0) {
        $("#search-submit").click(function (e) {
            var searchKeyword = encodeURIComponent($.trim($("#search-key").val())),
                searchParam = ($("#search-key").data("param") != "") ? "k=" + searchKeyword + "&" + $("#search-key").data("param") : "k=" + searchKeyword;
            if (searchKeyword == "") {
                baiaimama.Toast.New('请输入芝士标题或话题标签！');
                return false;
            }
            $("#list-wrapper").data("url", $("#search-key").data("url"));
            $("#list-wrapper").data("param", searchParam);
            $("#list-wrapper").data("index", 0);
            initlist(1);
        });
    }

    //初始化list
    function initlist() {
        $("#c-list-index").val(0);
        var list = $(".c-list-page"),
            apiUrl = $("#c-list-url").val() ? $("#c-list-url").val() : "",
            apiPraram = $("#c-list-param").val() ? $("#c-list-param").val() : "",
            apiIndex = 0;
        if (apiUrl == "") return false;
        $.ajax({
            type: 'get',
            url: apiUrl,
            data: (apiPraram == "") ? ("index=" + apiIndex) : (apiPraram + "&index=" + apiIndex),
            contentType: "application/json",
            success: function (data) {
                var res = $.parseJSON(data);
                if (res.result == false) {
                    baiaimama.Toast.New(res.msg);
                    return false;
                }
                if (res.result == true && res.data == null) {
                    list.children("ul").hide();
                    list.children(".no-result").show();
                    baiaimama.dropload.lock('down');
                    $(".dropload-down").hide();
                    baiaimama.dropload.resetload();
                    return false;
                }
                if (!(res.result == false) && !(res.data == null)) {
                    list.children(".no-result").hide();
                    list.children("ul").html(buildlist(res)).show();
                    resetSize($(".c-list-left"));
                    $("#c-list-index").val(1);
                    $('img.lazy').lazyload();
                    setDropload($('#cheese-list'), buildlist);
                    if (res.data.lists.length < 10) {
                        $(".dropload-down").hide();
                        baiaimama.dropload.lock('down');
                        baiaimama.dropload.resetload();
                    }
                }
            },
            error: function (xhr, type) {
                baiaimama.Toast.New('网络异常！');
            }
        });
    }

    //生成内容list
    function buildlist(res) {
        if (!res) return false;
        var result = '';
        for (var i = 0; i < res.data.lists.length; i++) {
            var link = res.data.lists[i].link ? res.data.lists[i].link : "",
                pic = res.data.lists[i].pic ? res.data.lists[i].pic : "",
                type = res.data.lists[i].type ? res.data.lists[i].type : "article",
                title = res.data.lists[i].title ? res.data.lists[i].title : "",
                description = res.data.lists[i].desc ? res.data.lists[i].desc : "",
                views = res.data.lists[i].views ? res.data.lists[i].views : 0,
                comments = res.data.lists[i].comments ? res.data.lists[i].comments : 0;
            result += '<li class="c-list-item" data-href="' + link + '">'
                        + '<div class="flex-wrapper flex-align-center">'
                            + '<div class="c-list-left">'
                                + '<img class="lazy" src="' + pic + '" alt=""/>'
                                 + '<div class="mask flex-wrapper flex-align-center">'
                                    + '<p class="icon i-' + type + '"></p>'
                               + ' </div>'
                            + '</div>'
                            + '<div class="c-list-right flex-1">'
                                + '<p class="c-list-title interception">' + title + '</p>'
                                + '<p class="c-list-desc interception">' + description + '</p>'
                            + '</div>'
                        + '</div>'
                   + '</li>';
        }
        return result;
    }
});